package backTrack;

import java.util.ArrayList;
import java.util.List;

/**
 * 全排列
 */
public class P46 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> total = new ArrayList<>();
        backtack(nums, nums.length, new ArrayList<>(), total);
        return total;
    }

    public void backtack(int[] nums, int len, List<Integer> subIndx, List<List<Integer>> total) {
        if (len == 0) {
            List<Integer> list = new ArrayList<>();
            subIndx.stream().forEach(a -> {
                list.add(nums[a]);
            });
            total.add(list);
        } else {
            for (int i = 0; i < nums.length; i++) {
                if (!subIndx.contains(i)) {
                    subIndx.add(i);
                    backtack(nums, len - 1, subIndx, total);
                    subIndx.remove(subIndx.size() - 1);
                }

            }
        }
    }

    public static void main(String[] args) {
        List<List<Integer>> permute = new P46().permute(new int[]{1});
        for (List<Integer> list : permute) {
            System.out.println(list);
        }
    }
}
